//
//  XMAdvertisementView.m
//  XMKit
//
//  Created by Ce Liu on 2018/5/29.
//  Copyright © 2018年 ChenXiaoMing. All rights reserved.
//

#import "XMAdvertisementView.h"

// View
#import "XMAdvertisementCell.h"

static NSString *const kXMAdvertisementCell = @"XMAdvertisementCell";

@interface XMAdvertisementView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) UIButton *adButton;

@property (nonatomic, strong) UIWindow *window;

@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation XMAdvertisementView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
        
        NSInteger pageNum = [self.urlNum integerValue] > 0 ? [self.urlNum integerValue] : 5;
        
        NSMutableArray *tempAry = @[
                                    @"https://api.swisstimevip.com/start/start_01.jpg",
                                    @"https://api.swisstimevip.com/start/start_02.jpg",
                                    @"https://api.swisstimevip.com/start/start_03.jpg",
                                    @"https://api.swisstimevip.com/start/start_04.jpg",
                                    @"https://api.swisstimevip.com/start/start_05.jpg",
                                    ].mutableCopy;
        
        self.dataSource = [NSMutableArray arrayWithArray:[tempAry subarrayWithRange:NSMakeRange(0, pageNum)]];
        
        self.pageControl.numberOfPages = self.dataSource.count;
        
        [self addSubview:self.pageControl];
        
        [self addSubview:self.adButton];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.pageControl.frame = CGRectMake(0, kSCREENHEIGHT - TabbarHeight, kSCREENWIDTH, 20);
    self.adButton.frame = CGRectMake(kSCREENWIDTH - 115, kSCREENHEIGHT - 200, 80, 35);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger currentIndex = scrollView.contentOffset.x / kSCREENWIDTH;
    self.pageControl.currentPage = currentIndex;
    if (currentIndex == self.dataSource.count - 1) {
        self.adButton.hidden = NO;
    } else {
        self.adButton.hidden = YES;
    }
}

- (void)show {
    UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    ///设置为最顶层，防止 AlertView 等弹窗的覆盖
    window.windowLevel = UIWindowLevelStatusBar + 1;

    ///默认为YES，当你设置为NO时，这个Window就会显示了
    window.hidden = NO;

    [window addSubview:self];

    ///防止释放，显示完后  要手动设置为 nil
    self.window = window;
    
    
//    UIWindow *window = [self lastWindow];
//    [window addSubview:self];
}

- (void)pushHomeClick {
    !_pushBlock ? : _pushBlock();
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *gridcell = nil;
    XMAdvertisementCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kXMAdvertisementCell forIndexPath:indexPath];
    [cell.advertisementImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.dataSource[indexPath.item]]] placeholderImage:nil];
    gridcell = cell;
    return gridcell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(kSCREENWIDTH, kSCREENHEIGHT);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UIButton *)adButton {
    if (!_adButton) {
        _adButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _adButton.backgroundColor = [UIColor colorWithHexString:@"333333" alpha:0.7];
        _adButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_adButton setTitle:@"进入首页" forState:UIControlStateNormal];
        [_adButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_adButton addTarget:self action:@selector(pushHomeClick) forControlEvents:UIControlEventTouchUpInside];
        _adButton.hidden = YES;
    }
    return _adButton;
}

- (UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] init];
        // 设置图片
//        [_pageControl setValue:[UIImage imageNamed:@"icon_bannerdefault"] forKeyPath:@"_pageImage"];
//        [_pageControl setValue:[UIImage imageNamed:@"icon_select"] forKeyPath:@"_currentPageImage"];
        _pageControl.currentPageIndicatorTintColor = [UIColor colorWithHexString:@"00bfff" alpha:1];
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    }
    return _pageControl;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionHeadersPinToVisibleBounds = NO;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = NO;
        [_collectionView registerClass:[XMAdvertisementCell class] forCellWithReuseIdentifier:kXMAdvertisementCell];
    }
    return _collectionView;
}


@end
